package com.keli.dt.dao.impl;

import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.JdbcDaoSupport;

import com.keli.common.DetectClass;
import com.keli.dt.dao.IDetectDeviceDao;
import com.keli.dt.model.DetectDevice;
import com.keli.dt.model.rowmapper.DetectDeviceRowMapper;

public class DetectDeviceDao extends JdbcDaoSupport implements
		IDetectDeviceDao {
	private Logger logger = Logger.getLogger(this.getClass());

	@Override
	public String findDetectClassById(String id) {
		String sql = "SELECT DETECT_CLASS FROM DT_DETECT_DECIVES_B WHERE DETECT_ID = ?";

		logger.debug(sql);

		try {
			// 仅查询一列，使用queryForObject
			String detectClassId = this.getJdbcTemplate().queryForObject(sql,
					String.class, id);	// 有可能id不存在
			return DetectClass.getDetectClassByKey(detectClassId);
		} catch (DataAccessException e) {
			return null;
		}
	}

	@Override
	public DetectDevice findById(String id) {
		String sql = "SELECT * FROM DT_DETECT_DECIVES_B WHERE DETECT_ID = ?";

		logger.debug(sql);
		
		List<DetectDevice> detectDevices = this.getJdbcTemplate().query(sql,
				new DetectDeviceRowMapper(), id);
		
		if (detectDevices != null && detectDevices.size() > 0) {
			return detectDevices.get(0);
		}
		
		return null;
	}

	@Override
	public List<DetectDevice> findAll() {
		String sql = "SELECT * FROM DT_DETECT_DECIVES_B";

		logger.debug(sql);
		
		return this.getJdbcTemplate().query(sql, new DetectDeviceRowMapper());
	}

}
